<script lang="ts" setup>
import { getCaseList, getHotTag } from '@/api/content/case'
import defaultItemImage from '@/assets/image/public/caseorabilitydefault.jpg'
import OneSelectTag from '@/component/content/selectTag/OneSelectTag.vue'
const searchform = ref({
  label: 'all',
  caseName: ''
})
const currentPage = ref(1)
const pageSize = ref(12)
const total = ref(0)
const caseDatas = ref([])
const labelList = ref([{ label: '全部', value: 'all' }])

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getCaseLists()
}
const getCaseLists = async (page = currentPage.value) => {
  console.log(searchform.value)
  const ret = await getCaseList({
    caseName: searchform.value.caseName,
    currentPage: page,
    pageSize: pageSize.value,
    sort: '',
    dictIds: searchform.value.label === 'all' ? [] : [searchform.value.label]
  })
  // console.log(ret);
  if (ret.retCode === '000000') {
    total.value = ret.data.total
    caseDatas.value = ret.data.list
  }
}

const getHotTags = async () => {
  const ret = await getHotTag()
  if (ret.retCode === '000000') {
    for (let i = 0; i < ret.data.length; i++) {
      labelList.value.push({
        value: ret.data[i].dictId,
        label: ret.data[i].dictName
      })
    }
  }
}

const truncateString = (str: string, n: number) => {
  return str?.length > n ? str.substr(0, n - 1) : str
}

onMounted(() => {
  getCaseLists()
  getHotTags()
})
</script>

<template>
  <div class="case-container">
    <div class="banner">
      <div class="content">
        <div class="title">应用场景库</div>
        <div class="describe">
          坚持以客户为中心的理念，不断快速响应、探索、挖掘、引领客户的需求，是市场竞争中最核心的能力和持续发展的关键因素。
        </div>
        <div class="search-input">
          <input type="text" class="search-box" v-model="searchform.caseName" placeholder="搜索应用场景库" />
          <button class="search-button" @click="getCaseLists(1)">搜索</button>
        </div>
      </div>
    </div>
    <div class="content">
      <div class="search-container">
        <el-form label-width="80px">
          <el-form-item label="热门标签:">
            <OneSelectTag v-model="searchform.label" :list="labelList" @change="getCaseLists(1)" />
          </el-form-item>
        </el-form>
      </div>
      <div v-if="caseDatas.length === 0">
        <el-empty description="暂无数据"></el-empty>
      </div>
      <div v-else></div>
      <div class="case-list">
        <div class="new-case-item" v-for="(item, index) in caseDatas" :key="index">
          <div class="new-case-imgs">
            <img :src="item.caseInfo.caseImgurl || defaultItemImage" alt="" />
          </div>
          <h3>{{ item.caseInfo.caseName }}</h3>
          <p v-html="truncateString(item.caseInfo.caseAbstract, 43)"></p>
          <div class="new-case-item-btns">
            <a
              class="new-case-item-detail"
              :class="{ singleitem: item.caseInfo.previewUrl === '' }"
              :href="'/case/' + item.caseInfo.id"
              target="_blank">
              查看详情
            </a>
            <a class="new-case-item-try" v-if="item.caseInfo.previewUrl !== ''" :href="item.caseInfo.previewUrl" target="_blank">
              体验试用
            </a>
          </div>
        </div>
      </div>
      <div class="pagination">
        <el-pagination
          class="justify-center"
          :current-page="currentPage"
          :page-size="pageSize"
          @current-change="handleCurrentChange"
          layout="total, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.case-container {
  font-family: MiSans-Regular, MiSans;
  font-weight: 400;
  background: #fff;
  min-height: 700px;

  .content {
    width: 1200px;
    margin: 0 auto;
    padding-bottom: 60px;
  }
}

.banner {
  height: 320px;
  width: 100%;
  background-image: url('@/assets/image/content/case/caseBanner.png');
  background-size: auto 320px;
  background-repeat: repeat;
  background-position: center;

  .content {
    padding-top: 60px;
  }

  .title {
    font-size: 42px;
    color: #1d263d;
    line-height: 56px;
  }

  .describe {
    margin-top: 16px;
    width: 600px;
    height: 44px;
    font-size: 14px;
    color: #747882;
    line-height: 22px;
  }

  /* 搜索框容器样式 */
  .search-input {
    margin-top: 92px;
    display: flex;
    align-items: center;
    width: 389px;
    height: 36px;
    //border: 1px solid #ccc;
    border-radius: 5px;
    overflow: hidden;

    .search-box {
      flex: 1;
      border: none;
      padding: 10px;
      font-size: 14px;
      outline: none;

      &::placeholder {
        font-size: 14px;
        color: #b6b6b8;
        line-height: 19px;
      }
    }

    .search-button {
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 0 5px 5px 0;
      padding: 10px 15px;
      cursor: pointer;
    }

    .search-button:hover {
      background-color: #0056b3;
    }
  }
}

.search-container {
  margin-top: 26px;

  .el-form-item {
    margin-bottom: 0px;
  }

  ::v-deep() {
    .el-form-item__label {
      font-size: 14px;
      font-family: MiSans-Regular, MiSans;
      font-weight: 600;
      color: #383e47;
    }
  }
}

.case-list {
  margin-top: 40px;

  &::after {
    content: '';
    display: table;
    clear: both;
  }

  .new-case-item {
    width: 286px;
    height: 317px;
    background: #ffffff;
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    float: left;
    margin-right: 18px;
    margin-bottom: 40px;
    padding: 12px 14px 19px 12px;

    .new-case-imgs {
      width: 260px;
      height: 160px;
      overflow: hidden;

      img {
        width: 260px;
        height: 100%;
      }
    }

    &:nth-of-type(4n) {
      margin-right: 0;
    }

    h3 {
      font-size: 16px;
      font-family: MiSans-Medium, MiSans-Medium;
      font-weight: normal;
      color: #383e47;
      line-height: 16px;
      margin-top: 16px;
      margin-bottom: 11px;
    }

    p {
      font-size: 14px;
      font-family: MiSans, MiSans;
      font-weight: 300;
      color: #989898;
      line-height: 19px;
      margin-bottom: 16px;
      height: 38px;
      overflow: hidden;
      word-break: break-all;
    }

    .new-case-item-btns {
      display: flex;
      justify-content: center;

      a {
        width: 124px;
        height: 28px;
        border-radius: 5px 5px 5px 5px;
        //opacity: 0.8;
        line-height: 28px;
        text-align: center;
        display: inline-block;
      }

      .new-case-item-detail {
        background: #007bff;
        color: #fff;
        margin-right: 12px;

        &.singleitem {
          width: 260px;
          margin-right: 0;
        }
      }

      .new-case-item-try {
        background: #ffffff;
        //opacity: 0.8;
        color: #007bff;
        border: 1px solid #007bff;
      }
    }
  }

  .new-case-item:hover {
    border-radius: 4px 4px 4px 4px;
    opacity: 1;
    outline: 1px solid #3e8efd;
  }
}

.pagination {
  text-align: center;
  margin-top: 40px;
}

.slide-up {
  animation: slide-up 0.5s ease-in-out forwards;
  transition: all 0.5s ease-in-out;
}

.slide-down {
  animation: slide-out-bottom 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

@keyframes slide-up {
  from {
    transform: translateY(33px);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes slide-out-bottom {
  0% {
    -webkit-transform: translateY(0);
    transform: translateY(0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translateY(33px);
    transform: translateY(33px);
    opacity: 0;
  }
}
</style>
